package com.xxxx.service.impl;
import java.time.LocalDateTime;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.dto.PageBean;
import com.xxxx.mapper.EmployeeMapper;
import com.xxxx.mapper.MailLogMapper;
import com.xxxx.pojo.Employee;
import com.xxxx.pojo.MailConstants;
import com.xxxx.pojo.MailLog;
import com.xxxx.pojo.R;
import com.xxxx.service.EmployeeService;
import com.xxxx.service.MailLogService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chao
 * @since 2022-09-26
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private MailLogMapper mailLogMapper;
    /**
     * 获取所有员工（分页）
     * @param currentPage
     * @param pageSize
     * @param employee
     * @param beginDateScope
     * @return
     */
    @Override
    public PageBean getEmployeeByPage(Integer currentPage, Integer pageSize, Employee employee, LocalDate[] beginDateScope) {
        Page<Employee> page = new Page<>(currentPage, pageSize);
        IPage<Employee> employeeIPage = employeeMapper.getEmployeeByPage(page, employee, beginDateScope);
        PageBean pageBean = new PageBean(employeeIPage.getTotal(), employeeIPage.getRecords());
        return pageBean;

    }
    /**
     * 返回最大工号
     * @return
     */
    @Override
    public String maxWorkId() {
        List<Map<String, Object>> maps = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workId)"));
        String idStr = maps.get(0).get("max(workId)").toString();
        int maxId = Integer.parseInt(idStr) + 1;
        String workId = String.format("%08d", maxId);
        return workId;
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @Override
    public R saveEmp(Employee employee) {
        LocalDate begin = employee.getBeginContract();
        LocalDate end = employee.getEndContract();
        long days = begin.until(end, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");

        employee.setContractTerm( Double.parseDouble(decimalFormat.format(days / 365.00)));
        if (employeeMapper.insert(employee) > 0) {
            Employee emp = employeeMapper.getEmployee(employee.getId()).get(0);
            String msgId = UUID.randomUUID().toString();
            MailLog mailLog = new MailLog();
            mailLog.setMsgId(msgId);
            mailLog.setEid(employee.getId());
            mailLog.setStatus(0);
            mailLog.setRouteKey(MailConstants.MAIL_ROUTING_KEY_NAME);
            mailLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
            mailLog.setCount(0);
            mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstants.MSG_TIMEOUT));
            mailLog.setCreateTime(LocalDateTime.now());
            mailLog.setUpdateTime(LocalDateTime.now());
            mailLogMapper.insert(mailLog);
            rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME, MailConstants.MAIL_ROUTING_KEY_NAME,
                    emp, new CorrelationData(msgId));
            return R.success("添加成功！");
        }
        return R.error("添加失败！");

    }

    @Override
    public List<Employee> getEmployee(Integer id) {
     return    employeeMapper.getEmployee(id);

    }

    @Override
    public PageBean getEmployeeWithSalary(Integer currentPage, Integer size) {

        Page<Employee> page = new Page<>(currentPage, size);
       IPage<Employee> employeePage= employeeMapper.getEmployeeWithSalary(page);
        PageBean pageBean = new PageBean(employeePage.getTotal(),employeePage.getRecords());
        return pageBean;

    }
}
